package com.sell.demo.mapper.good;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.sell.demo.entity.Good;

/**
 * 城市 DAO 接口类
 *
 * Created by xchunzhao on 02/05/2017.
 */
@Mapper // 标志为 Mybatis 的 Mapper
public interface GoodMapper {

    /**
     * 根据城市名称，查询城市信息
     *
     * @param cityName 城市名
     */
//    @Select("SELECT * FROM city")
    // 返回 Map 结果集
//    @Results({
//            @Result(property = "id", column = "id"),
//            @Result(property = "provinceId", column = "province_id"),
//            @Result(property = "cityName", column = "city_name"),
//            @Result(property = "description", column = "description"),
//    })
//    City findByName(@Param("cityName") String cityName);
	
	
	/**
     * 获取商品列表信息
     *
     * @param 
     */
    @Select("SELECT * FROM good")
     //返回 Map 结果集
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "type", column = "type"),
    })
    List<Good> getGoodList();
    
    /**
     * 获取商品列表信息
     *
     * @param 
     */
    @Select("SELECT * FROM good where id = #{id}")
     //返回 Map 结果集
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "type", column = "type"),
    })
    Good getGoodDetail(@Param("id") String id);
    
    /**
     * 创建商品信息
     * @param good
     */
    @Insert("insert into good(id,name,type) "
    		+ "values "
    		+ "(#{item.id},#{item.name},#{item.type})")
	int createGood(@Param("item") Good good);
	
	/**
     * 更新商品信息
     * @param good
     */
    @Update("update good set id = #{item.id},name=#{item.name},type=#{item.type} where id = #{item.id}")
	int updateGood(@Param("item") Good good);
	
	/**
     * 删除商品信息
     * @param good
     */
    @Delete("delete from good where id = #{id}")
	int deleteGood(@Param("id") String id);
	
	/**
     * 批量创建商品信息
     * @param good
     */
    @Insert("<script>"
    		+ "insert into good(id,name,type) "
    		+ "values "
    		+ "<foreach collection =\"list\" item=\"item\" index= \"index\" separator =\",\">"
    		+ "(#{item.id},#{item.name},#{item.type})"
    		+ "</foreach >"
    		+ "</script>")
	int createBatchGood(@Param("list")List<Good> goodList);
	
	/**
     * 批量更新商品信息
     * @param good
     */
	int updateBatchGood(@Param("list")List<Good> goodList);
	
	/**
     * 批量删除商品信息
     * @param good
     */
	@Delete("<script>"
			+ "delete from good where id in "
			+ "(<foreach collection =\"list\" item=\"id\" index= \"index\" separator =\",\">"
			+ "#{id}"
			+ "</foreach>)"
			+ "</script>"
			)
	int deleteBatchGood(@Param("list")String[] ids);
}
